function varargout = createChecklist(modelName, checklist, openExisting)
%createChecklist Create checklist from template
%   Make a copy of the checklist from its template.
%
%   createChecklist(ModelName, 'model review checklist')
%   createChecklist(ModelName, 'code review checklist')
%   createChecklist(ModelName, 'sim tests review checklist')
%   createChecklist(ModelName, 'eoc tests review checklist')
%   createChecklist(ModelName, 'sim results review checklist')
%   createChecklist(ModelName, 'eoc results review checklist')
%   createChecklist(ModelName, 'model review checklist', true)  % Open existing mode
%
%   Inputs:
%   modelName - 模型名称
%   checklist - 检查清单类型
%   openExisting - (可选) 如果为true，仅打开已存在的文件；如果为false或未指定，创建新文件
%
%   基于DO-178规则第2条：明确报告错误，不使用silent fallback
%   基于DO-178规则第6条：使用writeExcelProtection处理Excel文件锁定问题

%   

% 参数验证
if nargin < 2
    error('createChecklist:InvalidInput', '至少需要两个输入参数: modelName 和 checklist');
end

% 默认为创建模式
if nargin < 3
    openExisting = false;
end

% 确定源文件和目标文件路径
switch checklist
    case 'model review checklist'
        srcChecklist = fullfile(ProjectPathManager.getDirPath('checklists'), 'Model_Review_Checklist.xlsx');
        dstChecklist = fullfile(ProjectPathManager.getDirPath('model review', modelName), [ProjectNamingManager.getNameStr(checklist, modelName), '.xlsx']);
    case 'code review checklist'
        srcChecklist = fullfile(ProjectPathManager.getDirPath('checklists'), 'Code_Review_Checklist.xlsx');
        dstChecklist = fullfile(ProjectPathManager.getDirPath('code review', modelName), [ProjectNamingManager.getNameStr(checklist, modelName), '.xlsx']);
    case 'sim tests review checklist'
        srcChecklist = fullfile(ProjectPathManager.getDirPath('checklists'), 'Simulation_Cases_Procedures_Review_Checklist.xlsx');
        dstChecklist = fullfile(ProjectPathManager.getDirPath('test cases', modelName), [ProjectNamingManager.getNameStr(checklist, modelName), '.xlsx']);
    case 'eoc tests review checklist'
        srcChecklist = fullfile(ProjectPathManager.getDirPath('checklists'), 'Test_Cases_Procedures_Review_Checklist.xlsx');
        dstChecklist = fullfile(ProjectPathManager.getDirPath('sim results', modelName), [ProjectNamingManager.getNameStr(checklist, modelName), '.xlsx']);
    case 'sim results review checklist'
        srcChecklist = fullfile(ProjectPathManager.getDirPath('checklists'), 'Simulation_Results_Review_Checklist.xlsx');
        dstChecklist = fullfile(ProjectPathManager.getDirPath('test cases', modelName), [ProjectNamingManager.getNameStr(checklist, modelName), '.xlsx']);
    case 'eoc results review checklist'
        srcChecklist = fullfile(ProjectPathManager.getDirPath('checklists'), 'Test_Results_Review_Checklist.xlsx');
        dstChecklist = fullfile(ProjectPathManager.getDirPath('test results', modelName), [ProjectNamingManager.getNameStr(checklist, modelName), '.xlsx']);
    otherwise
        error('createChecklist:InvalidChecklistType', ...
              '不支持的检查清单类型: %s', checklist);
end

% 验证模板文件存在
if ~exist(srcChecklist, 'file')
    error('createChecklist:TemplateNotFound', '模板文件不存在: %s', srcChecklist);
end

% 确保目标目录存在
targetDir = fileparts(dstChecklist);
if ~exist(targetDir, 'dir')
    fprintf('创建目标目录: %s\n', targetDir);
    mkdir(targetDir);
end

% 根据openExisting参数处理文件操作
success = false;
if openExisting
    % 打开模式：尝试打开已存在的文件
    if exist(dstChecklist, 'file') == 2
        fprintf('打开检查清单: %s\n', dstChecklist);
        winopen(dstChecklist);
        result.Message = ['已打开现有检查清单: ', [ProjectNamingManager.getNameStr(checklist, modelName), '.xlsx']];
    else
        % 文件不存在，询问用户是否创建
        answer = questdlg('检查清单不存在，是否创建新的检查清单？', ...
            'Checklist Not Found', ...
            'Yes', 'No', 'Yes');
        if strcmp(answer, 'Yes')
            % 基于规则第6条：使用writeExcelProtection处理可能的Excel文件锁定
            writeExcelProtection(srcChecklist, dstChecklist);
            fprintf('成功创建新的检查清单: %s\n', dstChecklist);
            winopen(dstChecklist);
            success = true;
            result.Message = ['新检查清单 ', [ProjectNamingManager.getNameStr(checklist, modelName), '.xlsx'], ' 创建成功。'];
        else
            fprintf('用户取消创建检查清单操作\n');
            result.Message = '用户取消操作';
            if nargout > 0
                varargout{1} = result;
            end
            return;
        end
    end
else
    % 创建模式：创建新文件或覆盖已存在的文件
    if exist(dstChecklist, 'file') == 2
        % 文件已存在，询问用户是否覆盖
        answer = questdlg('检查清单已存在，是否创建新的检查清单？', ...
            'Checklist Reset Permission', ...
            'Yes', 'No', 'No');
        if strcmp(answer, 'Yes')
            % 基于规则第6条：使用writeExcelProtection处理Excel文件锁定问题
            writeExcelProtection(srcChecklist, dstChecklist);
            success = true;
        else
            fprintf('用户取消覆盖检查清单操作\n');
            result.Message = ['现有检查清单 ', [ProjectNamingManager.getNameStr(checklist, modelName), '.xlsx'], ' 已保留。'];
            if nargout > 0
                varargout{1} = result;
            end
            disp(result.Message);
            return;
        end
    else
        % 文件不存在，直接复制
        copyfile(srcChecklist, dstChecklist, 'f');
        fprintf('成功创建新的检查清单: %s\n', dstChecklist);
        success = true;
    end
    
    % 打开新创建的文件
    fprintf('打开检查清单: %s\n', dstChecklist);
    winopen(dstChecklist);
end

% 设置返回消息
if success
    result.Message = ['新检查清单 ', [ProjectNamingManager.getNameStr(checklist, modelName), '.xlsx'], ' 创建成功。'];
else
    if ~exist('result', 'var')
        result.Message = ['现有检查清单 ', [ProjectNamingManager.getNameStr(checklist, modelName), '.xlsx'], ' 已保留。'];
    end
end

if nargout > 0
    varargout{1} = result;
end
disp(result.Message);

end
